﻿using System.DirectoryServices;

namespace Hx.ADSyncPlatform.ActiveDirectory.Access
{
    /// <summary>
    /// 域控访问
    /// </summary>
    internal class DomainControllerAccess
    {

        //hack:这个方法的用途？
        /// <summary>
        ///自定义查询
        /// </summary>
        /// <param name="filter">LDAP查询语句</param>
        /// <param name="propertiesToLoad">需要返回的属性</param>
        /// <param name="searchScope">查询层级</param>
        /// <param name="pagination">分页信息，NULL时返回所有</param>
        /// <returns>返回域控SearchResultCollection</returns>
        //public SearchResultCollection Search(string filter, List<string> propertiesToLoad, SearchScope searchScope, Pagination pagination)
        //{
        //    DirectorySearcher dsearcher = new DirectorySearcher(rootEntry, filter, propertiesToLoad?.ToArray(), searchScope);
        //    if(pagination!=null)
        //    {
        //        dsearcher.PageSize = pagination.PageIndex * pagination.PageSize;
        //    }
        //    SearchResultCollection searchResult = dsearcher.FindAll();
        //    return searchResult;
        //}

        //hack：获取当前域下所有域控，可以考虑两种实现方式：1、PowerShell；2、查询OU【DomainController】下的计算机
        /// <summary>
        /// 查询组织下的域控
        /// </summary>
        /// <param name="org">组织</param>
        /// <param name="searchScope">查询层级</param>
        /// <returns>返回域控SearchResultCollection</returns>
        public SearchResultCollection Get(DirectoryEntry org, SearchScope searchScope)
        {
            string filter = "(objectclass=Computer)";
            DirectorySearcher dsearcher = new DirectorySearcher(org, filter, null, SearchScope.Subtree);
            SearchResultCollection result = dsearcher.FindAll();
            return result;
        }
    }
}
